package com.zy.fee.roadpark.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;
import com.zy.fee.roadpark.R;
import com.zygk.library.base.BaseActivity;
import com.zygk.library.util.ToastUtil;
import java.util.List;

public class H5Activity extends BaseActivity {
  public static final String INTENT_URL = "INTENT_URL";
  public static final String INTENT_TITLE = "INTENT_URL_TITLE";

  @BindView(R.id.webView) WebView webView;
  @BindView(R.id.lh_tv_title) TextView lhTvTitle;

  private String url, title;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
  }

  @Override
  public void setContentView() {
    setContentView(R.layout.activity_h5);
  }

  @Override public void init() {
    initData();
    initView();
    initListener();
  }

  public void initData() {
    url = getIntent().getStringExtra(INTENT_URL);
    title = getIntent().getStringExtra(INTENT_TITLE);


    initWebView();

    if (AndPermission.hasPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)) {
      webView.loadUrl(url);
    } else {
      requestLocationPermission();
    }
  }

  public void initView() {
    lhTvTitle.setText(title);

  }

  public void initListener() {
  }

  private void initWebView() {
    webView.clearCache(true);
    String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDatabaseEnabled(true);
    webSettings.setGeolocationEnabled(true);
    webSettings.setGeolocationDatabasePath(dir);
    webSettings.setDomStorageEnabled(true);

    webView.setWebViewClient(new WebViewClient() {
      @Override
      public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
      }

      @Override public void onLoadResource(WebView view, String url) {
      }

      @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
      }
    });

    webView.setWebChromeClient(new WebChromeClient() {
      @Override public void onReceivedIcon(WebView view, Bitmap icon) {
        super.onReceivedIcon(view, icon);
      }

      @Override
      public void onGeolocationPermissionsShowPrompt(String origin,
          GeolocationPermissions.Callback callback) {
        final boolean remember = true;
        AlertDialog.Builder builder = new AlertDialog.Builder(H5Activity.this);
        builder.setTitle("位置信息");
        builder.setMessage(origin + "允许获取您的位置信息吗？").setCancelable(true).setPositiveButton("允许",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog,
                  int id) {
                callback.invoke(origin, true, remember);
              }
            })
            .setNegativeButton("不允许",
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog,
                      int id) {
                    callback.invoke(origin, false, remember);
                  }
                });
        AlertDialog alert = builder.create();
        alert.show();
      }
    });
  }

  private void requestLocationPermission() {
    AndPermission.with(this)
        .requestCode(REQUEST_CODE_LOCATION)
        .permission(Manifest.permission.ACCESS_FINE_LOCATION)
        .callback(this)
        .start();
  }

  @PermissionYes(REQUEST_CODE_LOCATION)
  private void getPermissionYes(List<String> grantedPermissions) {
    webView.loadUrl(url);
  }

  @PermissionNo(REQUEST_CODE_LOCATION) private void getPermissionNo(List<String> deniedPermissions) {
    ToastUtil.showMessage(mContext, "无定位权限, 请开启定位权限");
  }

  @OnClick({
      R.id.ll_back
  }) public void onViewClicked(View view) {
    Intent intent;
    switch (view.getId()) {
      case R.id.ll_back:
        finish();
        break;
    }
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
    }
  }
}
